package gw.com.android.ui.views;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.design.widget.TabLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.lang.reflect.Field;

import gw.com.android.app.AppMain;
import gw.com.android.ui.account.LoginActivity;
import www.com.library.app.AppActivities;
import www.com.library.util.CommonUtils;
import www.com.library.util.DeviceUtil;
import www.com.library.view.BtnClickListener;
import www.com.library.view.TintImageView;
import www.com.library.view.TintTextView;

/**
 * 公共顶部title自定义控件，带返回按钮
 *
 * @author jett
 */
public class CommonTitleBar extends LinearLayout implements OnClickListener {

    private View mSystemTitleBar;
    public View mAppTitleBar;
    public TextView mTitleView; // 中间文案
    public View mDividerView;
    public TabLayout tabs;

    public TintImageView mLeftIcon; // 左边按钮
    public TintImageView mLeftSecondaryIcon; // 左二按钮
    public TintTextView mLeftText;
    public TintTextView mLeftSendaryText;

    public TintImageView mRightIcon; // 右边按钮
    public TintImageView mRightSecondaryIcon; // 右边按钮
    public TintTextView mRightText;
    public TintTextView mRightSendaryText;

    private BtnClickListener mCallback;// 左按钮点击事件监听器

    private TextView mTvQuoteTitleName; //行情中文文案
    private TextView mTvQuoteTitleAliasName;    //行情英文文案
    private TintImageView mIvLeftArrow; //行情左边箭头
    private TintImageView mIvRightArrow;    //行情右边箭头

    public CommonTitleBar(Context context) {
        super(context);
        initView(context);
    }

    public CommonTitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    public CommonTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public void initView(Context act) {
        View mView = LayoutInflater.from(act).inflate(R.layout.common_title_bar, this, true);
        mSystemTitleBar = mView.findViewById(R.id.system_title_bar);
        mAppTitleBar = mView.findViewById(R.id.app_title_bar);
        mTitleView = (TextView) mView.findViewById(R.id.title_name);
        mDividerView = mView.findViewById(R.id.title_divider);
        tabs = (TabLayout) mView.findViewById(R.id.title_tabs);

        mLeftIcon = (TintImageView) mView.findViewById(R.id.title_left_icon);
        mLeftSecondaryIcon = (TintImageView) mView.findViewById(R.id.title_left_secondary_icon);
        mLeftText = (TintTextView) mView.findViewById(R.id.title_left_text);
        mLeftSendaryText = (TintTextView) mView.findViewById(R.id.title_left_sendary_text);

        mRightIcon = (TintImageView) mView.findViewById(R.id.title_right_icon);
        mRightSecondaryIcon = (TintImageView) mView.findViewById(R.id.title_right_sendary_icon);
        mRightText = (TintTextView) mView.findViewById(R.id.title_right_text);
        mRightSendaryText = (TintTextView) mView.findViewById(R.id.title_right_sendary_text);

        mTvQuoteTitleName = (TextView) mView.findViewById(R.id.tv_title_name);
        mTvQuoteTitleAliasName = (TextView) mView.findViewById(R.id.tv_title_alias_name);
        mIvLeftArrow = (TintImageView) mView.findViewById(R.id.tv_left_arrow);
        mIvRightArrow = (TintImageView) mView.findViewById(R.id.tv_right_arrow);

        mLeftIcon.setOnClickListener(this);
        mLeftSecondaryIcon.setOnClickListener(this);
        mLeftText.setOnClickListener(this);
        mLeftSendaryText.setOnClickListener(this);
        mRightIcon.setOnClickListener(this);
        mRightSecondaryIcon.setOnClickListener(this);
        mRightText.setOnClickListener(this);
        mRightSendaryText.setOnClickListener(this);
        mIvLeftArrow.setOnClickListener(this);
        mIvRightArrow.setOnClickListener(this);

        //setUpIndicatorWidth(tabs, 10, 10);
    }

    private void setUpIndicatorWidth(TabLayout tabLayout, int marginLeft, int marginRight) {
        try {
            Class<?> tabLayoutClass = tabLayout.getClass();
            Field tabStrip = tabLayoutClass.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
            LinearLayout layout = (LinearLayout) tabStrip.get(tabLayout);

            for (int i = 0; i < layout.getChildCount(); i++) {
                View child = layout.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                params.leftMargin = DeviceUtil.instance().dip2px(marginLeft, getContext());
                params.rightMargin = DeviceUtil.instance().dip2px(marginRight, getContext());
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (Throwable ignore) {
        }
    }

    /**
     * 设置中间title文案
     */
    public void setAppTitle(int appTitleID) {
        setAppTitle(AppMain.getApp().getString(appTitleID));
    }

    /**
     * 设置中间title文案
     */
    public void setAppTitle(CharSequence appTitle) {
        mTitleView.setVisibility(VISIBLE);
        mTitleView.setText(appTitle);
    }

    /**
     * 左一 图标
     */
    public void setLeftIcon(int resId) {
        mLeftIcon.setImageResource(resId);
    }

    /**
     * 左一 状态颜色
     */
    public void setLeftColorValue(int normal, int selected) {
        mLeftIcon.setColorValue(normal, selected);
    }

    /**
     * 左一 文字
     */
    public void setLeftText(int resId) {
        setLeftText(AppMain.getApp().getString(resId));
    }

    /**
     * 左一 文字
     */
    public void setLeftText(String text) {
        mLeftIcon.setVisibility(GONE);
        mLeftText.setVisibility(VISIBLE);
        mLeftText.setText(text);
    }

    /**
     * 左二 图标
     */
    public void setLeftSendaryIcon(int resId) {
        if (resId == -1) {
            mLeftSecondaryIcon.setVisibility(GONE);
        } else {
            mLeftSecondaryIcon.setVisibility(VISIBLE);
            mLeftSecondaryIcon.setImageResource(resId);
        }
    }

    /**
     * 左二 状态颜色
     */
    public void setLeftSendaryColorValue(int normal, int selected) {
        mLeftSecondaryIcon.setColorValue(normal, selected);
    }

    /**
     * 左二 文字
     */
    public void setLeftSendaryText(int resId) {
        setLeftSendaryText(AppMain.getAppString(resId));
    }

    /**
     * 左二 文字
     */
    public void setLeftSendaryText(String text) {
        mLeftSendaryText.setVisibility(VISIBLE);
        mLeftSendaryText.setText(text);
    }

    /**
     * 右一 图标
     */
    public void setRightIcon(int resId) {
        mRightIcon.setVisibility(VISIBLE);
        mRightIcon.setImageResource(resId);
    }

    public ImageView getRightIconView(){
        return mRightIcon;
    }

    /**
     * 右一 状态颜色
     */
    public void setRightColorValue(int normal, int selected) {
        mRightText.setColorValue(normal, selected);
    }

    /**
     * 右一 文字
     */
    public void setRightText(int resId) {
        setRightText(AppMain.getAppString(resId));
    }

    /**
     * 右一 文字
     */
    public void setRightText(String text) {
        mRightText.setVisibility(VISIBLE);
        mRightText.setText(text);
    }

    /**
     * 右二 图标
     */
    public void setRightSendaryIcon(int resId) {
        mRightSecondaryIcon.setVisibility(VISIBLE);
        mRightSecondaryIcon.setImageResource(resId);
    }

    /**
     * 右二 文字
     */
    public void setRightSendaryText(int resId) {
        setRightSendaryText(AppMain.getAppString(resId));
    }

    /**
     * 右二 文字
     */
    public void setRightSendaryText(String text) {
        mRightSendaryText.setVisibility(VISIBLE);
        mRightSendaryText.setText(text);
    }

    /**
     * 设置 title bar 背景颜色
     */
    public void setTitleBarBackgroundColor(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mAppTitleBar.setBackgroundColor(color);
            mSystemTitleBar.setBackgroundColor(color);
        }
    }

    /**
     * 行情title
     */
    public void setQuoteTitleText(String title, String alias) {
        View quoteLayout = findViewById(R.id.layout_quote_title);
        if (quoteLayout.getVisibility() != VISIBLE) {
            quoteLayout.setVisibility(VISIBLE);
        }
        mDividerView.setVisibility(GONE);
        mTvQuoteTitleName.setText(title);
        mTvQuoteTitleAliasName.setText(alias);
    }

    /**
     * 设置行情左边箭头状态
     */
    public void setQuoteLeftArrowStatus(int visibility) {
        mIvLeftArrow.setVisibility(visibility);
    }

    /**
     * 设置行情右边箭头状态
     */
    public void setQuoteRightArrowStatus(int visibility) {
        mIvRightArrow.setVisibility(visibility);
    }

    public void addTab(String tabName, boolean selected) {
        tabs.setVisibility(VISIBLE);
        tabs.addTab(tabs.newTab().setText(tabName), selected);
    }

    public void setTabSelectedListener(TabLayout.OnTabSelectedListener listener) {
        tabs.setOnTabSelectedListener(listener);
    }

    public void onNightMode(boolean isNight) {
        if (isNight) {
            mSystemTitleBar.setBackgroundColor(getResources().getColor(R.color.color_night_bg));
            mAppTitleBar.setBackgroundColor(getResources().getColor(R.color.color_night_bg));
            mTitleView.setTextColor(getResources().getColor(R.color.color_f));
            mDividerView.setBackgroundColor(getResources().getColor(R.color.color_night_bg_dark));
            mTvQuoteTitleName.setTextColor(getResources().getColor(R.color.color_f));
            mTvQuoteTitleAliasName.setTextColor(getResources().getColor(R.color.color_f));
        } else {
            mSystemTitleBar.setBackgroundColor(getResources().getColor(R.color.color_nav_bg));
            mAppTitleBar.setBackgroundColor(getResources().getColor(R.color.color_nav_bg));
            mTitleView.setTextColor(getResources().getColor(R.color.color_nav_title));
            mDividerView.setBackgroundColor(getResources().getColor(R.color.color_d));
            mTvQuoteTitleName.setTextColor(getResources().getColor(R.color.color_b));
            mTvQuoteTitleAliasName.setTextColor(getResources().getColor(R.color.color_b));
        }
        mLeftIcon.onNightMode(isNight);
        mLeftSecondaryIcon.onNightMode(isNight);
        mRightText.onNightMode(isNight);
        mRightIcon.onNightMode(isNight);
        mRightSecondaryIcon.onNightMode(isNight);
        mIvLeftArrow.onNightMode(isNight);
        mIvRightArrow.onNightMode(isNight);
    }

    /**
     * 设置按钮点击监听事件
     */
    public void setBtnClickListener(BtnClickListener mCallback) {
        this.mCallback = mCallback;
    }

    @Override
    public void onClick(View v) {
        if (CommonUtils.isFastDoubleClick())
            return;
        switch (v.getId()) {
            case R.id.title_left_icon:
                if (null != mCallback) {
                    mCallback.onBtnClick(v.getId());
                } else {
                    Activity currentActivity = AppActivities.getSingleton().currentActivity();
                    if (currentActivity != null && currentActivity instanceof LoginActivity) {
                        return;
                    }
                    ((Activity) getContext()).finish();
                }
                break;
            default:
                if (null != mCallback) {
                    mCallback.onBtnClick(v.getId());
                }
                break;
        }

    }

    public static abstract class SimpleTabSelectedListener implements TabLayout.OnTabSelectedListener {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            onTabSelected(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }

        protected abstract void onTabSelected(int position);

    }

}
